package cn.tedu.tmall.front.content.service.impl;

import cn.tedu.tmall.common.enumerator.ServiceCode;
import cn.tedu.tmall.common.ex.ServiceException;
import cn.tedu.tmall.common.pojo.authentication.CurrentPrincipal;
import cn.tedu.tmall.common.pojo.vo.PageData;
import cn.tedu.tmall.front.content.dao.persist.repository.IArticleRepository;
import cn.tedu.tmall.front.content.dao.persist.repository.ICommentRepository;
import cn.tedu.tmall.front.content.pojo.entity.Comment;
import cn.tedu.tmall.front.content.pojo.param.ArticleCommentAddNewParam;
import cn.tedu.tmall.front.content.pojo.vo.ArticleStandardVO;
import cn.tedu.tmall.front.content.pojo.vo.CommentListItemVO;
import cn.tedu.tmall.front.content.service.ICommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 处理评论数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 3.0
 */
@Slf4j
@Service
public class CommentServiceImpl implements ICommentService {

    @Value("${tmall.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private ICommentRepository commentRepository;
    @Autowired
    private IArticleRepository articleRepository;

    public CommentServiceImpl() {
        log.info("创建业务对象：CommentServiceImpl");
    }

    @Override
    public void addNewArticleComment(CurrentPrincipal currentPrincipal, String remoteAddr, ArticleCommentAddNewParam articleCommentAddNewParam) {
        log.debug("开始处理【发表文章评论】的业务，参数：{}", articleCommentAddNewParam);

        Long articleId = articleCommentAddNewParam.getArticleId();
        ArticleStandardVO article = articleRepository.getStandardById(articleId);
        if (article == null) {
            String message = "发表文章评论失败，文章数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        Comment comment = new Comment();
        BeanUtils.copyProperties(articleCommentAddNewParam, comment);
        comment.setAuthorId(currentPrincipal.getId());
        comment.setAuthorName(currentPrincipal.getUsername());
        comment.setIp(remoteAddr);
        comment.setFloor(article.getCommentCount() + 1);
        comment.setUpCount(0);
        comment.setDownCount(0);
        comment.setCheckState(CHECK_STATE_UNCHECKED);
        comment.setIsDisplay(DISPLAY_STATE_OFF);
        int rows = commentRepository.insert(comment);
        if (rows != 1) {
            String message = "发表文章评论失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }

        rows = articleRepository.setCommentCount(articleId, article.getCommentCount() + 1);
        if (rows != 1) {
            String message = "发表文章评论失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public PageData<CommentListItemVO> listByArticle(Long articleId, Integer pageNum) {
        log.debug("开始处理【根据文章查询评论列表】的业务，文章：{}, 页码：{}", articleId, pageNum);
        return commentRepository.listByArticle(articleId, pageNum, defaultQueryPageSize);
    }

    @Override
    public PageData<CommentListItemVO> listByArticle(Long articleId, Integer pageNum, Integer pageSize) {
        log.debug("开始处理【根据文章查询评论列表】的业务，文章：{}, 页码：{}，每页记录数：{}", articleId, pageNum, pageSize);
        return commentRepository.listByArticle(articleId, pageNum, pageSize);
    }

}
